var index;
var slider_value = -1;
var timeIndex;
var whitelist_id;
var idcard;
/**----------------黑广播查询页面-----------------------*/
//-------------检索表单------------------------
layui.use('form', function () {
    var form = layui.form;
    //监听提交
    form.on('submit(selectFrom)', function (data) {
        let msg = JSON.stringify(data.field);
        console.log(msg);
        var table = layui.table;
        table.reload('blackbroad1_table', {
            where: {
                '0': msg
            },
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
        return false;
    });
});
//-------------开始时间 -----------------------
layui.use('laydate', function () {
    var laydate = layui.laydate;
    //执行一个laydate实例
    laydate.render({
        elem: '#starttime', //指定元素
        theme: '#337AB7',
        type: 'datetime'
    });
});
//-------------结束时间　----------------------
layui.use('laydate', function () {
    var laydate = layui.laydate;
    laydate.render({
        elem: '#endtime', //指定元素
        theme: '#337AB7',
        type: 'datetime'
    });
});
//-------------滑块 ---------------------------
layui.use('slider', function () {
    var slider = layui.slider;
    //渲染
    slider.render({
        elem: '#slideTest1', //绑定元素
        step: 10,
        max: 365,
        setTips: function (value) { //自定义提示文本
            $('#text').html(getDay(-value) + '日之后的广播');
            slider_value = value;
            return value + "天之内"
        }
    });
});
//-------------黑广播表格１--------------------
layui.use(['table', 'layer'], function () {
    var table = layui.table;
    //第一个实例
    table.render({
        elem: '#tb_blackbroad1',
        url: '/d/broadcast/text', //数据接口
        id: 'blackbroad1_table',
        toolbar: true,
        defaultToolbar: ['filter', 'print', 'exports'],
        parseData: function (res) {
            return {
                'data': res.data,
                'count': res.count,
                'code': 0
            }
        },
        page: {
            elem: 'page_wh',
            theme: '#337AB7',
            limit: 10,
            layout: ['count', 'prev', 'page', 'next', 'skip'],
        },
        cols: [
            [ //表头
                {
                    field: 'freq',
                    title: '频点',
                    event: 'none',
                    width: 70
                },
                {
                    field: 'category__name',
                    title: '内容',
                    event: 'none',
                    width: 80
                },
                {
                    field: 'time',
                    title: '发现时间',
                    event: 'none',
                    width: 100,
                    sort: true
                },
                {
                    field: 'mobile__name',
                    title: '上报设备',
                    event: 'none',
                    width: 80
                },
                {
                    field: 'address',
                    title: '发现地点',
                    event: 'none',
                    width: 100,
                    templet: function (res) {
                        if (res.address === '[]' || res.address === 'null') {
                            return '无具体地址'
                        } else {
                            return res.address
                        }
                    }
                },
                {
                    field: 'contact',
                    title: '联系方式',
                    event: 'none',
                    width: 80,
                    edit: "text"
                },
                {
                    field: 'common',
                    title: '备注',
                    event: 'none',
                    width: 80,
                    edit: "text"
                },
                {
                    field: 'record',
                    title: '音频',
                    width: 60,
                    event: 'redio',
                    templet: function (res) {
                        // layer.alert(res.record);
                        axios.get(res.record).then(function (response) {
                            // layer.alert(response.data.code);
                            // let v = response.data;
                            if (response.data.code === '0') {
                                let icon = document.getElementById(res.id);
                                icon.className = 'layui-icon layui-icon-close';
                                // alert(icon.className);
                            }
                        }).catch(function (error) {
                            console.log(error);
                            let icon = document.getElementById(res.id);
                            icon.className = 'layui-icon layui-icon-close';
                        });
                        return '<i id ="' + res.id + '" class= "layui-icon layui-icon-play"></i>';
                    }
                },
                {
                    field: 'delete',
                    title: '修改',
                    event: 'none',
                    toolbar: '#edit_info',
                    fixed: "right"
                },
            ]
        ],
    });
    table.on('tool(tb_blackbroad1)', function (obj) {
        // alert(obj.event);
        if (obj.event === 'redio') {
            // alert(obj.data.record);
            axios
                .get(obj.data.record)
                .then(function (response) {
                    if (response.data.code !== 'no') {
                        layer.open({
                            title: '录音',
                            content: '<audio src = "' + obj.data.record + '" controls = "controls" >浏览器不支持音频</audio>',
                            // content: '!',
                            offset: '100px'
                        });
                    }
                });
        } else if (obj.event === "edit") {
            let contact = obj.data.contact;
            let common = obj.data.common;
            let id = obj.data.id;
            let params = {
                "contact": contact,
                "common": common,
                "id": id
            };
            layer.confirm("确定修改么", function (index) {
                axios
                    .patch('/d/broadcast/text', params)
                    .then(function (response) {
                        let data = response.data.msg;
                        layer.alert(data);
                    });
                layer.close(index);
            });
            // layer.alert(JSON.stringify(params));
        } else {
            map.clearMap();
            var lnglat = obj.data.lnglat.split(",");
            var lnla = new AMap.LngLat(lnglat[0], lnglat[1]);
            var marker = new AMap.Marker({
                position: lnla,
            });
            map.add(marker);
            map.setCenter(lnla);
            map.setZoom(16.33);
        }
    });
});
//-------------黑广播表格2---------------------
layui.use(['table', 'layer'], function () {
    var table = layui.table;
    //第一个实例
    table.render({
        elem: '#tb_blackbroad2',
        url: '/d/broadcast/text', //数据接口
        id: 'blackbroad2_table',
        toolbar: true,
        defaultToolbar: ['filter', 'print', 'exports'],
        parseData: function (res) {
            return {
                'data': res.data,
                'count': res.count,
                'code': 0
            }
        },
        page: {
            elem: 'page_wh',
            theme: '#337AB7',
            limit: 10,
            layout: ['count', 'prev', 'page', 'next', 'skip'],
        },
        cols: [
            [ //表头
                {
                    field: 'freq',
                    title: '频点',
                    event: 'none',
                    width: '10%'
                },
                {
                    field: 'category__name',
                    title: '内容',
                    event: 'none',
                    width: '10%'
                },
                {
                    field: 'time',
                    title: '发现时间',
                    event: 'none',
                    width: '20%',
                    sort: true
                },
                {
                    field: 'mobile__name',
                    title: '上报设备',
                    event: 'none',
                    width: '10%'
                },
                {
                    field: 'address',
                    title: '发现地点',
                    event: 'none',
                    width: '45%',
                    templet: function (res) {
                        if (res.address === '[]' || res.address === 'null') {
                            return '无具体地址'
                        } else {
                            return res.address
                        }
                    }
                },
                {
                    field: 'record',
                    title: '音频',
                    width: 60,
                    event: 'redio',
                    templet: function (res) {
                        axios.get(res.record).then(function (response) {
                            // layer.alert(response.data.code);
                            if (response.data.code === '0') {
                                let icon = document.getElementById('region/' + res.id);
                                icon.className = 'layui-icon layui-icon-close';
                            }
                        }).catch(function (error) {
                            console.log(error);
                            let icon = document.getElementById('region/' + res.id);
                            icon.className = 'layui-icon layui-icon-close';
                        });
                        return '<i id ="region/' + res.id + '" class= "layui-icon layui-icon-play"></i>';
                    }
                },
                {
                    field: 'contact',
                    title: '联系方式',
                    event: 'none',
                    width: 80
                },
                {
                    field: 'common',
                    title: '备注',
                    event: 'none',
                    width: 80
                },
            ]
        ],
    });
    table.on('tool(tb_blackbroad2)', function (obj) {
        // alert(obj.event);
        if (obj.event === 'redio') {
            axios
                .get(obj.data.record)
                .then(function (response) {
                    if (response.data.code !== 'no') {
                        layer.open({
                            title: '录音',
                            content: '<audio src = "' + obj.data.record + '" controls = "controls" >浏览器不支持音频</audio>',
                            // content: '!',
                            offset: '100px'
                        });
                    }
                });
        }
    });
    // layer.open({
    //         title: '在线调试'
    //         , content: '可以填写任意的layer代码'
    //     }
    // )
});
/**----------------工作管理页面-----------------------*/
//-------------移动端表格----------------------
var phoneid_curr;
layui.use(['table', 'layer'], function () {
    let table = layui.table;
    //第一个实例
    table.render({
        elem: '#tb_MobileList',
        url: '/d/control/mobile?mobile=1', //数据接口
        id: 'MobileList_table',
        toolbar: true,
        defaultToolbar: ['filter', 'print', 'exports', {
            title: '添加移动端' //标题
                ,
            layEvent: 'addMobile' //事件名，用于 toolbar 事件中使用
                ,
            icon: 'layui-icon-add-circle' //图标类名
        }],
        parseData: function (res) {
            return {
                'data': res.data,
                'count': res.mobile_length,
                'code': 0
            }
        },
        page: {
            elem: 'page_wh',
            theme: '#337AB7',
            limit: 5,
            layout: ['count', 'prev', 'page', 'next', 'skip'],
        },
        cols: [
            [ //表头
                {
                    field: 'name',
                    title: '移动端名称',
                    event: 'none',
                    edit: 'text'
                },
                {
                    field: 'phonenumber',
                    title: '手机号',
                    event: 'none',
                    edit: 'text'
                },
                {
                    field: 'district',
                    title: '所属地区',
                    event: 'none'
                },
                {
                    field: 'time',
                    title: '加入时间',
                    event: 'none'
                },
                {
                    field: 'workpath',
                    title: '工作路径(近12小时)',
                    event: 'none',
                    toolbar: '#barDemo1'
                },
                {
                    field: 'delete',
                    title: '操作',
                    event: 'none',
                    toolbar: '#barDemo2'
                },
            ]
        ],
    });
    table.on('tool(tb_MobileList)', function (obj) {
        let data = obj.data;
        let layEvent = obj.event;
        if (layEvent === 'work') {
            phoneid_curr = data.mobile;
            timeIndex = layer.open({
                title: '工作路径',
                type: 1,
                content: $('#mobile_workpath'),
                area: ['600px','500px']
            });
        } else if (layEvent === 'del') {
            layer.confirm('确定删除么', function (index) {
                obj.del();
                var params = {
                    'mobile': data.mobile
                };
                axios
                    .delete('/d/control/mobile', {
                        data: params
                    }).then(function (response) {
                        let msg = response.data.msg;
                        layer.alert(msg);
                    });
                layer.close(index);
            });
        } else if (layEvent === 'edit') {
            phoneid_curr = data.mobile;
            layer.open({
                title: '修改移动端',
                type: 1,
                id: 'timeLayer',
                content: $('#mobile_edit'),
                area: '360px'
            });
        }
    });
    table.on('toolbar(tb_MobileList)', function (obj) {
        let layEvent = obj.event;
        // alert(layEvent);
        if (layEvent === 'addMobile') {
            layer.open({
                title: '添加移动端',
                type: 1,
                id: 'timeLayer',
                content: $('#mobile_add'),
                area: '360px'
            });
        }
    })
});
//-------------工作路径时间范围　----------------------
layui.use('laydate', function () {
    let laydate = layui.laydate;
    laydate.render({
        elem: '#workRange', //指定元素
        theme: '#337AB7',
        type: 'datetime',
        range: true
    });
});
//------------- 工作路径表单提交 ------------------
layui.use(['form', 'layer'], function () {
    let form = layui.form;
    //监听提交
    form.on('submit(timeRange)', function (data) {
        let con = data.field;
        let timerange = con["timeRange"].split(" - ");
        let params = {
            "s_time": timerange[0],
            "e_time": timerange[1],
            "mobile": phoneid_curr
        };
        layer.close(timeIndex);
        axios
            .post("/d/mobile/wrokpath", params)
            .then(function (response) {
                let info = response.data;
                let location_list = info.data;
                path(location_list);
            })
            .catch(function (error) {
                console.log(error);
            });
        return false;
    });
});
// ----------------- 获取当前时间字符串 ------------------------
function get_now_time_str() {
    let myDate = new Date();
    let y = myDate.getFullYear();
    let m = myDate.getMonth() + 1;
    let d = myDate.getDate();
    let H = myDate.getHours();
    let M = myDate.getMinutes();
    let S = myDate.getSeconds();
    let time_str = y + "-" + m + "-" + d + " " + H + ":" + M + ":" + S;
    return time_str;
}
//------------- 移动端添加表单 ------------------
layui.use(['form', 'layer', 'table'], function () {
    let form = layui.form;
    let table = layui.table;
    //监听提交
    form.on('submit(formDemo)', function (data) {
        let time_str = get_now_time_str();
        let params = data.field;
        params["time"] = time_str;
        axios
            .post('/d/control/mobile', params)
            .then(function (response) {
                let msg = response.data.msg;
                layer.alert(msg);
                table.reload("MobileList_table");
                return false;
            });
        return false;
    });
});
//------------- 移动端修改表单提交 ------------------
layui.use(['form', 'layer', 'table'], function () {
    let form = layui.form;
    //监听提交
    form.on('submit(moEditSubmit)', function (data) {
        let params = data.field;
        let table = layui.table;
        params["mobile"] = phoneid_curr;
        axios
            .patch('/d/control/mobile', params)
            .then(function (response) {
                let msg = response.data.msg;
                layer.alert(msg);
                table.reload("MobileList_table");
                return false
            });
        return false;
    });
});
//------------- 白名单表格 ----------------------
layui.use(['table', 'layer'], function () {
    let table = layui.table;
    //第一个实例
    table.render({
        elem: '#tb_whiteList',
        url: '/d/whitelist', //数据接口
        id: 'whiteList_table',
        toolbar: true,
        defaultToolbar: ['filter', 'print', 'exports', {
            title: '添加白名单' //标题
                ,
            layEvent: 'addWhiteList' //事件名，用于 toolbar 事件中使用
                ,
            icon: 'layui-icon-add-circle' //图标类名
        }],
        parseData: function (res) {
            count = res.count;
            return {
                'data': res.data,
                'count': res.count,
                'code': 0
            }
        },
        page: {
            elem: 'page_wh',
            theme: '#337AB7',
            limit: 15,
            layout: ['count', 'prev', 'page', 'next', 'skip']
        },
        cols: [
            [ //表头
                {
                    field: 'freq',
                    title: '频点',
                    event: 'none',
                    width: 200
                },
                {
                    field: 'name',
                    title: '名称',
                    event: 'none',
                    width: 200
                },
                {
                    field: 'time',
                    title: '加入时间',
                    event: 'none',
                    width: 200
                },
                {
                    field: 'district',
                    title: '区域',
                    event: 'none',
                    width: 250
                },
                {
                    field: 'delete',
                    title: '删除',
                    event: 'none',
                    toolbar: '#barDemo2',
                    width: 200
                },
            ]
        ],
    });
    table.on('tool(tb_whiteList)', function (obj) {
        let data = obj.data;
        let layEvent = obj.event;
        if (layEvent === 'del') {
            layer.confirm('确定删除么', function (index) {
                let params = {
                    'id': data.id
                };
                axios
                    .delete('/d/whitelist', {
                        data: params
                    }).then(function (response) {
                        layer.alert(response.data.msg);
                        obj.del();
                    });
                layer.close(index);
            });
        } else if (layEvent === 'edit') {
            whitelist_id = data.id;
            index = layer.open({
                content: $("#whitelist_edit"),
                title: "修改白名单",
                offset: 100,
                type: 1,
                area: "360px"
            });
        }
    });
    table.on('toolbar(tb_whiteList)', function (obj) {
        let layEvent = obj.event;
        if (layEvent === 'addWhiteList') {
            index = layer.open({
                content: $('#whitelist_add'),
                title: '添加白名单',
                offset: 100,
                type: 1,
                area: '360px'
            });
        }
    })
});
//-------------白名单添加表单------------------
layui.use(['form', 'layer', 'table'], function () {
    let form = layui.form;
    let table = layui.table;
    //监听提交
    form.on('submit(wh_post)', function (data) {
        let time_str = get_now_time_str();
        let params = data.field;
        params["time"] = time_str;
        params["phoneid"] = "111111111111111";
        let dis_id = params["district"];
        if (dis_id === "0") {
            params["type"] = 1;
        } else {
            params["type"] = 3;
        }
        axios
            .post('/d/whitelist', params)
            .then(function (response) {
                layer.close(index);
                layer.alert(response.data.msg);
                table.reload('whiteList_table');
            })
            .catch(function (error) {
                console.log(error)
            });
        return false;
    });
});
//----------------- 白名单修改表单 ---------------------
layui.use(["form", "layer", "table"], function () {
    let form = layui.form;
    let table = layui.table;
    form.on("submit(wl_edit)", function (data) {
        let params = data.field;
        params["id"] = whitelist_id;
        axios
            .patch("/d/whitelist", params)
            .then(function (response) {
                let msg = response.data.msg;
                layer.close(index);
                layer.alert(msg);
                table.reload('whiteList_table');
                return false;
            });
        return false;
    });
});
//-------------检测人员名单表格----------------------
layui.use(['table', 'layer'], function () {
    let table = layui.table;
    //第一个实例
    table.render({
        elem: '#tb_workList',
        url: '/d/control/monitor', //数据接口
        id: 'workList_table',
        toolbar: true,
        defaultToolbar: ['filter', 'print', 'exports', {
            title: '添加检测人员' //标题
                ,
            layEvent: 'addWorkList' //事件名，用于 toolbar 事件中使用
                ,
            icon: 'layui-icon-add-circle' //图标类名
        }],
        parseData: function (res) {
            return {
                'data': res.data,
                'count': res.count,
                'code': 0
            }
        },
        page: {
            // elem: 'page_wh',
            theme: '#337AB7',
            limit: 15,
            layout: ['count', 'prev', 'page', 'next', 'skip'],
        },
        cols: [
            [ //表头
                {
                    field: 'name',
                    title: '姓名',
                    event: 'none',
                    width: 250,
                },
                {
                    field: 'idcard',
                    title: '身份证',
                    event: 'none',
                    width: 250
                },
                {
                    field: 'district',
                    title: '所属地区',
                    event: 'none',
                    width: 250
                },
                {
                    field: 'time',
                    title: '加入时间',
                    event: 'none',
                    width: 250
                },
                {
                    field: 'delete',
                    title: '删除',
                    event: 'none',
                    toolbar: '#barDemo2',
                    width: 250
                }
            ]
        ],
    });
    table.on('tool(tb_workList)', function (obj) {
        let data = obj.data;
        let layEvent = obj.event;
        // alert(layEvent);
        if (layEvent === 'del') {
            layer.confirm('确定删除么', function () {
                let params = {
                    'idcard': data.idcard
                };
                axios
                    .delete('/d/control/monitor', {
                        data: params
                    }).then(function (response) {
                        let msg = response.data.msg;
                        layer.alert(msg);
                        table.reload('workList_table');
                    });
                layer.close(index);
            });
        } else if (layEvent === 'edit') {
            idcard = data.idcard;
            index = layer.open({
                content: $("#workListEdit"),
                title: "修改白名单",
                offset: 100,
                type: 1,
                area: "360px"
            });
            // layer.confirm('确定修改么', function (index) {
            //     let params = {
            //         'name': data.name,
            //         'idcard': data.idcard
            //     };
            //     // console.log(params);
            //     axios
            //         .patch('/d/control/monitor', params)
            //         .then(function (response) {
            //             let msg = response.data.msg;
            //             layer.alert(msg);
            //         });
            //     layer.close(index);
            // });
        }
    });
    table.on('toolbar(tb_workList)', function (obj) {
        let layEvent = obj.event;
        if (layEvent === 'addWorkList') {
            index = layer.open({
                content: $('#workListAdd'),
                title: '添加白名单',
                offset: 100,
                type: 1,
                area: '360px'
            });
        }
    })
});
//-------------检测人员添加表单------------------
layui.use(['form', 'layer', 'table'], function () {
    let form = layui.form;
    let table = layui.table;
    let layer = layui.layer;
    //监听提交
    form.on('submit(work_post)', function (data) {
        let params = data.field;
        params["time"] = get_now_time_str();
        axios
            .post('/d/control/monitor', params)
            .then(function (response) {
                let msg = response.data.msg;
                layer.close(index);
                layer.alert(msg);
                table.reload('workList_table');
            })
            .catch(function (error) {
                console.log(error)
            });
        return false;
    });
});
//-------------检测人员修改表单------------------
layui.use(['form', 'layer', 'table'], function () {
    let form = layui.form;
    let table = layui.table;
    let layer = layui.layer;
    //监听提交
    form.on('submit(work_edit)', function (data) {
        let params = data.field;
        params["idcard"]=idcard;
        console.log(params);
        // params["time"] = get_now_time_str();
        axios
            .patch('/d/control/monitor', params)
            .then(function (response) {
                let msg = response.data.msg;
                layer.close(index);
                layer.alert(msg);
                table.reload('workList_table');
            })
            .catch(function (error) {
                console.log(error)
            });
        return false;
    });
});
// ---------------- 设备使用记录表格 ----------------------
layui.use(["table", "layer"], function () {
    let table = layui.table;
    //第一个实例
    table.render({
        elem: '#tb_UseRecord_id',
        url: '/d/control/userecord', //数据接口
        id: 'tb_useRecord',
        toolbar: true,
        defaultToolbar: ['filter', 'print', 'exports'],
        parseData: function (res) {
            return {
                'data': res.data,
                'count': res.count,
                'code': 0
            }
        },
        cols: [
            [ //表头
                {
                    field: 'time',
                    title: '时间',
                    event: 'none',
                    width: 333
                },
                {
                    field: 'mobile',
                    title: '设备名称',
                    event: 'none',
                    width: 333
                },
                {
                    field: 'monitor',
                    title: '检测人',
                    event: 'none',
                    width: 333
                }
            ]
        ],
    });
});
// ---------------- 设备使用记录检索表单 ---------------
layui.use(["form", "layer"], function () {
    let form = layui.form;
    //监听提交
    form.on('submit(use_record_submit)', function (data) {
        console.log(data.field);
        // layer.alert(JSON.stringify(data.field));
        let table = layui.table;
        table.reload('tb_useRecord', {
            where: data.field,
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
        return false;
    });
});
/**------------------------ 设置页面 -------------------------*/
//------------- 系统设置表单 ----------------------
layui.use(['form', 'layer', 'laydate'], function () {
    let form = layui.form;
    let layer = layui.layer;
    form.on('submit(sysTimeRange)', function (data) {
        let content = data.field;
        axios.post('/d/control/systimerange', content).then(function (response) {
            let data = response.data.msg;
            layer.alert(data);
            timerange_reset();
            axios.get('/d/bigscreen/flushwebsocket');
        });

        // layer.alert(content.timeRange);
        return false;
    });
    form.on('submit(CHART_SET)', function (data) {
        let content = data.field;
        // layer.alert(JSON.stringify(content));
        axios
            .post('/d/control/systimerange', content)
            .then(function (response) {
                let data = response.data.msg;
                layer.alert(data);
                timerange_reset();
                axios.get('/d/bigscreen/flushwebsocket');
            });

        // // layer.alert(content.timeRange);
        return false;
    });
    form.on('submit(BROAD_SET)', function (data) {
        // layer.alert('broad');
        let content = data.field;
        axios.post('/d/control/systimerange', content).then(function (response) {
            let data = response.data.msg;
            layer.alert(data);
            timerange_reset();
            axios.get('/d/bigscreen/flushwebsocket');
        });

        // layer.alert(content.timeRange);
        return false;
    });
    form.on('submit(ISWORKING_SET)', function (data) {
        // layer.alert('isworking');
        let content = data.field;
        axios.post('/d/control/systimerange', content).then(function (response) {
            let data = response.data.msg;
            layer.alert(data);
            timerange_reset();
            axios.get('/d/bigscreen/flushwebsocket');
        });

        // layer.alert(content.timeRange);
        return false;
    });
});